<template>
<!-- <view><h2>555555</h2></view> -->
<!-- <view style="width: 100%;">

<view style="height: 200000px;">{{message}}</view>

</view> -->
<view class="">
   <!-- <button type="primary" open-type="getUserInfo" @getuserinfo="getuserinfo" withCredentials="true">微信登录</button> -->
    <loading v-if="showloading"></loading>
    <view class="center_bor" v-if="weizinlogin">
        <div class="titlep pb10">
            <div class="text">Hi，欢迎来到访客平台</div>
        </div>
        <div class="wechantLogin" @click="login">微信登录</div>
    </view>

</view>
</template>

<script>
import {
    mapState,
    mapMutations
} from 'vuex';
import mInput from '../../components/m-input.vue';
import graceChecker from '../../common/graceChecker.js';
import md5 from '../../common/md5.js';
import {
    isEmpty
} from '@/common/graceChecker.js';
export default {
    data() {
        return {
            token: '',
            message: '',
            showloading: true,
            weizinlogin: false,
        };
    },
    computed: mapState(['forcedLogin']),

    created() {
	
        // uni.showLoading({  
        //     title: '加载中'  
        // });
        // #ifdef H5 
        function getQueryString(name) {
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
        }

        let code = getQueryString('code');

        var local = this.weixinhttpurl+'/#/pages/shouquan/shouquan'; // 获取页面url
		var appid = this.$_GENV.GwechatAppid;
        if (code == null || code === '') {
            // 如果没有code，则去请求
            window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`;
        }
		
        uni.request({
            url: this.apihttpurl+'/wechat/login', //仅为示例，并非真实接口地址。
            data: {
                code: code
            },

            success: res => {
				sessionStorage.setItem("isloadingget",'YES');
                console.log(res.data);
				// alert(res.data.token)
				// alert(JSON.stringify(res.data))
				
                if (res.data.code == 400) {

                    window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`;
					// uni.showModal({
					// 	title: '',
					// 	content: '系统正在进行相关操作...',						
					// 	showCancel:false,
					// 	confirmText: "确定",
					// 	success: function (res) {
					// 		if (res.confirm) {
					// 		} else if (res.cancel) {
					// 			console.log('用户点击取消');
					// 		}
					// 	}
					// });

                }
				// var skippage=sessionStorage.getItem("skippage");
    //             if (res.data.subscribe == 0) {
    //                 // var href = 'https://mp.weixin.qq.com/s/HstHcezAdSlrZCVXMaZc8A';
				// 	// var href = 'https://mp.weixin.qq.com/s/kc64hSEoH7YCOHfDSY50Ng'; //智能访客
				// 	if(skippage=='YES'){
				// 		sessionStorage.setItem("subscribe",'YES')
				// 	}else{
				// 		var href = 'https://mp.weixin.qq.com/s/jopUdv6ICw6uxN3XB_3Wnw';
				// 		window.location.replace(href)
				// 	}
					
    //             }
	sessionStorage.setItem("subscribe",'')
	var skippage=sessionStorage.getItem("skippage");
	if (res.data.subscribe == 0) {
	    // var href = 'https://mp.weixin.qq.com/s/HstHcezAdSlrZCVXMaZc8A';
		// var href = 'https://mp.weixin.qq.com/s/kc64hSEoH7YCOHfDSY50Ng'; //智能访客
		if(skippage=='YES'){
			sessionStorage.setItem("subscribe",'YES')
		}else{
			//迎客舟
			// var href = 'https://mp.weixin.qq.com/s/jopUdv6ICw6uxN3XB_3Wnw';
			//智服网
			var href = 'https://mp.weixin.qq.com/s/kc64hSEoH7YCOHfDSY50Ng';
			window.location.replace(href)
		}
		
	}
				
				var skippage=sessionStorage.getItem("skippage");
				 if(res.data.isIdentitycard== 0&&skippage=='YES'||res.data.ismoblie== 0&&skippage=='YES'){ //判断是否跳转登录
					 uni.setStorage({
						 key: 'token',
						 data: res.data.token
					 });
					 var thisurl = sessionStorage.getItem("locationhref");
					 if (thisurl) {
					     var newhrf = "/" + thisurl;
						 if(skippage!='YES'){
							sessionStorage.setItem("locationhref",'');
							sessionStorage.setItem("skippage",'') 
						 }
					     
					     uni.reLaunch({
					         url: newhrf
					     });
					 }
					 if(res.data.ismoblie== 0){
						 sessionStorage.setItem("routing",'ismoblie');
					 }else if(res.data.isIdentitycard==0){
						sessionStorage.setItem("routing",'isIdentitycard');
					 }
				 }else{
					
					 uni.setStorage({
					     key: 'token',
					     data: res.data.token
					 });
					 
					 uni.setStorageSync('userid', res.data.userData.id);
					 uni.setStorage({
					     key: 'headimgurl',
					 
					     data: res.data.headimgurl
					 });
					 uni.setStorageSync('username', res.data.userData.name);
					 uni.setStorage({
					     key: 'isIdentitycard',
					     data: res.data.isIdentitycard
					 });
					 uni.setStorage({
					     key: 'userInfo',
					     data: res.data
					 });
					 
					if (res.data.ismoblie == 0) {
					  
					    uni.setStorage({
					        key: 'ismobliex',
					        data: res.data
					    });
					
					    uni.reLaunch({
					        url: '/pages/shouquan/shouji'
					    });
					} else if (res.data.isIdentitycard == 0) {
					
					    uni.setStorage({
					        key: 'isIdentitycard',
					        data: res.data.isIdentitycard
					    });
					
					    uni.reLaunch({
					        url: '/pages/shouquan/shengfen'
					    });
					} 
					// is_completeperson 1-简单注册，2-完整注册
					else if (res.data.is_completeperson == 2&&res.data.isPersonVerify == 0) { //人脸识别
						
					    uni.setStorage({
					        key: 'isIdentitycard',
					        data: res.data.isIdentitycard
					    });
					
					    uni.reLaunch({
					        url: '/pages/shouquan/shengfen'
					    });
					} else {
					    sessionStorage.setItem("tokensession", res.data.token);
					    var thisurl = sessionStorage.getItem("locationhref");
						if (thisurl) {
						    var newhrf = "/" + thisurl;
							if(skippage!='YES'){
								sessionStorage.setItem("locationhref",'');
								sessionStorage.setItem("skippage",'') 
							}
						    uni.reLaunch({
						        url: newhrf
						    });
						}else{
						    uni.reLaunch({
						        url: '/pages/user/user'
						    });
						}
						
					    setTimeout(() => {
					        this.weizinlogin = true;
					        this.showloading = false;
					    }, 3000)
					}
					 
				 }
				 
            }
        });

        // #endif

        // #ifdef MP-WEIXIN
        let userdata = []; // 获取用户信息
        uni.login({
            provider: 'weixin',
            success: function (loginRes) {
                // loginRes 实际输出的是  {"errMsg":"login:ok","code":"0230gxqx1BgRRh0afIox1UAOqx10gxqF"}
                // 并不存在 authResult 属性。  
                // 微信开发者工具此处提示 获取 wx.getUserInfo 接口后续将不再出现授权弹窗，请注意升级  

                console.log(loginRes)
                let code = loginRes.code;
                uni.getUserInfo({
                    provider: 'weixin',
                    success: function (infoRes) {
                        if (infoRes.errMsg == "getUserInfo:ok") {
                            userdata = infoRes.userInfo

                            uni.request({
                                url: this.apihttpurl+'/wechat/login', //仅为示例，并非真实接口地址。
                                data: {
                                    code: code,
                                    type: 'wx',
                                    avatarUrl: userdata.avatarUrl,
                                    city: userdata.city,
                                    gender: userdata.gender,
                                    nickName: userdata.nickName,
                                    province: userdata.province,
                                },

                                success: res => {
                                    console.log(res.data);

                                    // if(res.data.subscribe == 0 ){
                                    // 	window.location.href ='https://mp.weixin.qq.com/s/HstHcezAdSlrZCVXMaZc8A'
                                    // }

                                    // sessionStorage.setItem("tokensession", res.data.token); 
                                    uni.setStorage({
                                        key: 'token',
                                        data: res.data.token
                                    });

                                    uni.setStorageSync('userid', res.data.userData.id);
                                    uni.setStorage({
                                        key: 'headimgurl',

                                        data: res.data.headimgurl
                                    });
                                    uni.setStorageSync('username', res.data.userData.name);
                                    uni.setStorage({
                                        key: 'isIdentitycard',
                                        data: res.data.isIdentitycard
                                    });
                                    uni.setStorage({
                                        key: 'userInfo',
                                        data: res.data
                                    });

                                    if (res.data.ismoblie == 0) {
                                        uni.setStorage({
                                            key: 'ismobliex',
                                            data: res.data
                                        });

                                        uni.reLaunch({
                                            url: '/pages/shouquan/shouji'
                                        });
                                    } else if (res.data.isIdentitycard == 0) {
                                        uni.setStorage({
                                            key: 'isIdentitycard',
                                            data: res.data.isIdentitycard
                                        });

                                        uni.reLaunch({
                                            url: '/pages/shouquan/shengfen'
                                        });
                                    } else if (res.data.isPersonVerify == 0) {
                                        uni.setStorage({
                                            key: 'isIdentitycard',
                                            data: res.data.isIdentitycard
                                        });

                                        uni.reLaunch({
                                            url: '/pages/shouquan/shengfen'
                                        });
                                    } else {
                                       // var thisurl=uni.getStorageSync('locationhref');
                                         var thisurl=sessionStorage.getItem("locationhref");
                                        // var thisurl = ''
										
                                        if (thisurl) {
                                            var newhrf = "/" + thisurl;
                                             sessionStorage.setItem("locationhref",'');
                                            uni.reLaunch({
                                                url: newhrf
                                            });
                                        }else{
                                            uni.reLaunch({
                                                url: '/pages/user/user'
                                            });
                                        }

                                        setTimeout(() => {
                                            this.weizinlogin = true;
                                            this.showloading = false;
                                        }, 3000)
                                    }
                                }
                            });

                        }

                    },
                })

            },
            fail: function (es) {
                console.log('fail')
                console.log(es)
            }
        });
        // #endif

    },

    methods: {
        login() {
            // uni.reLaunch({
            // url: '/pages/shouquan/shouji'
            // });
			var local = 'https://' + this.$_GENV.Gdomain +'/wap/#/pages/shouquan/shouquan'; // 获取页面url
			var appid = this.$_GENV.GwechatAppid;
            window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(

local

)}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`;
        },
        getuserinfo() {
            // wx登录
            wx.login({
                success(res) {
                    if (res.code) {
                        //发起网络请求
                        const code = res.code;
                        // 获取微信用户信息
                        wx.getUserInfo({
                            success: function (res) {
                                console.log(res)
                            },
                            fail: res => {
                                // 获取失败的去引导用户授权
                                uni.showToast({
                                    title: '您需要授权，才能获取您的信息！'
                                });
                            }
                        });
                    } else {
                        //
                    }
                }
            });
        },
        onGotUserInfo(e) {
            // 获取用户信息
            uni.getUserInfo({
                // 获取信息成功
                success(res) {
                    console.log(res);
                    // 成功后进行登录,获取code
                },
                fail() {
                    console.log("获取用户信息失败");
                }
            })
        },
    }
};
</script>

<style lang="scss" scoped>
.center_bor {
    width: 100%;
    padding-top: 35%;
    text-align: center;

    // position: relative;
    .text {

        letter-spacing: 5px;
        color: rgba(80, 80, 80, 1);
        font-size: 26px;
        font-weight: bold;

    }
}

.wechantLogin {
    width: 80%;
    margin: 0 auto;
    height: 41px;
    background-color: rgba(80, 138, 203, 1);
    border-radius: 21px;
    letter-spacing: 5px;
    color: rgba(255, 255, 255, 1);
    font-size: 18px;
    line-height: 41px;
    text-align: center;
}
.loadingimg {width: 2.5rem;margin: 0 auto;}
</style>
